package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Portrait;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getusername")
    public Result getUserName(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user != null){
            //获取用户名
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }


    //通过用户名获取菜单
    @RequestMapping("/getmenu")
    public Result getMenu(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            List<Menu> map = menuService.getMenu(user.getUsername());
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }


    //显示头像
    @RequestMapping("/getimg")
    public Result getimg(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            String username = user.getUsername();
           Portrait portrait = menuService.getimg(username);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,portrait);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    //修改头像
    @RequestMapping("/edit")
    public Result edit(String img){
        if(img.equals("undefined")){
            return new Result(true,MessageConstant.UNDEFINED_PICTURE);
        }

        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            String username = user.getUsername();
           Map map = new HashMap();
           map.put("username",username);
           map.put("img",img);
            menuService.edit(map);
            return new Result(true,MessageConstant.UPLOAD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    //获取角色
    @RequestMapping("/role")
    public Result getrole(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            String username = user.getUsername();

           List<Map> list = menuService.getrole(username);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //通过用户名获取菜单
    @RequestMapping("/setRoleId")
    public Result setRoleId(Integer id){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            List<Menu> map = menuService.getMenu(user.getUsername(),id);
            return new Result(true,MessageConstant.SWITCHOVER_USER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SWITCHOVER_USER_FAIL);
        }
    }

    //回显数据到下拉框
    @RequestMapping("/roleName")
    public Result roleName(Integer id){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            String  name = menuService.getRoleName(user.getUsername());
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,name);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

}
